package com.woniuxy.oa.utils;
 
import javax.annotation.PostConstruct;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.woniuxy.oa.entity.Goods_claim;
import com.woniuxy.oa.entity.Goods_purchase;
import com.woniuxy.oa.mapper.GoodsRepositoryMapper;
import com.woniuxy.oa.mapper.Goods_claimMapper;
import com.woniuxy.oa.mapper.Goods_purchaseMapper;

@Component
public class GoodsRepositoryUtil {
	@Autowired
	Goods_claimMapper goods_claimMapper;
	@Autowired
	Goods_purchaseMapper goods_purchaseMapper;
	@Autowired
	GoodsRepositoryMapper goodsRepositoryMapper;
	
	private static Goods_claimMapper utilGoods_claimMapper;
	private static Goods_purchaseMapper utilGoods_purchaseMapper;
	private static GoodsRepositoryMapper utilGoodsRepositoryMapper;
	
	@PostConstruct
	public void init() {
		utilGoods_claimMapper=goods_claimMapper;
		utilGoods_purchaseMapper=goods_purchaseMapper;
		utilGoodsRepositoryMapper=goodsRepositoryMapper;
	}
	
	//申领：当审批通过后，根据申领主键id去更改库存的数量
	public static void updateGoodsRepositoryByGc_id(Integer gc_id) {
		//根据gc_id查询当前信息
		Goods_claim goods_claim = utilGoods_claimMapper.selectGoods_claimByGc_id(gc_id);
		//获取申领的物品名称
		String gc_name = goods_claim.getGc_name();
		//获取申领的物品数量
		Integer gc_number=goods_claim.getGc_number();
		//根据申领的名称去修改库存的数量
		utilGoodsRepositoryMapper.updateGr_numberByGr_name(gc_number,gc_name);
	}
	
	
	//申购：当审批通过后，根据申购主键id去更改库存的数量
		public static void updateGoodsRepositoryByGp_id(Integer gp_id) {
			//根据gp_id查询当前信息
			Goods_purchase goods_purchase = utilGoods_purchaseMapper.selectGoods_purchaseByGp_id(gp_id);
			//获取申领的物品名称
			String gp_name = goods_purchase.getGp_name();
			//获取申领的物品数量
			Integer gp_number = goods_purchase.getGp_number();
			//判断库存里有没有此物品
			Integer gr_number = utilGoodsRepositoryMapper.selectGr_numberByGr_name(gp_name);
			if(gr_number ==null) {
				//往库存添加数据
				utilGoodsRepositoryMapper.insertGoodsRepository(gp_name,gp_number,gp_id);
			}else {
				//增加库存数量
				utilGoodsRepositoryMapper.updateAddGr_numberByGr_name(gp_number, gp_name);
			}
		}
}
